package com.qcit.common.activity



import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.IdRes
import androidx.fragment.app.FragmentActivity
import com.qcit.base.callback.IBackListener
import com.qcit.base.utils.AppManager
import com.qcit.common.R
import com.qcit.common.utils.QSharedPreferences
import kotlinx.android.synthetic.main.activity_base.*
abstract class BaseActivity :FragmentActivity() {
    private var view: View? = null
    open fun layout(): Int{
        return 0
    }
    var back: IBackListener? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_base);
        addActivity() // 调用添加方法
        view = layoutInflater.inflate(layout(), null);
        root.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        initToolBar()
    }

    fun setTitleVisibility(@IdRes int:Int){
        if (toolbar.visibility == int)return
        toolbar.visibility = int
    }
    fun setRightText(txt:String?){
        if (!TextUtils.isEmpty(txt)){
            rt.visibility = View.VISIBLE;
            rt.text = txt
        }else{
            rt.visibility = View.GONE;
        }
    }
    fun setRightTextClickListener(listener: View.OnClickListener){
        rt.setOnClickListener(listener)
    }
    fun addMenu(title: String): View {
        val rl = RelativeLayout(this)
        var paramsrl = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        rl.layoutParams = paramsrl

        val tv = TextView(this)
        var params = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        params.addRule(RelativeLayout.CENTER_IN_PARENT)
        tv.layoutParams = params
        tv.text = title
        tv.textSize = 16.toFloat()
        tv.setTextColor(Color.parseColor("#555555"))

        rl.addView(tv)
        ln_menu.addView(rl)
        return tv
    }

    private fun initToolBar() {
        tvTitle.text = title
        ivBack.setOnClickListener {
            if (null != back){
                back?.setBackListener()
            }else{
                finish()
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    public fun setToolbarBackGone() {
        ivBack.visibility = View.GONE
    }

    public fun reSetTitle(title: String) {
        tvTitle.text = title
    }
    fun getCurrTitle():String{
        return tvTitle.text.toString()
    }

    fun toast(toast: String) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show()
    }

    fun toastNetError() {
        Toast.makeText(this, getText(R.string.err_network), Toast.LENGTH_SHORT).show()
        showLoading(false)
    }

    fun showLoading(show: Boolean) {
        if (show){
            loading.hide()
            loading.show()
            loading.visibility= View.VISIBLE
        }
        else {
            loading.hide()
            loading.visibility= View.GONE
        }
    }


    fun bgAlpha(f: Float) {
        val layoutParams = window.attributes
        layoutParams.alpha = f
        window.attributes = layoutParams
    }

    fun
            checkUser():Boolean{
        var user = QSharedPreferences.user
        if (user == null) {
            toast("请先登录")
            // startActivity(LoginActivity())
            finish()
            return false
        }else {
            return true
        }
    }

    // 添加Activity方法
    open fun addActivity() {
        AppManager.getAppManager().addActivity(this) // 添加Activity方法
    }

    //销毁当个Activity方法
    open fun removeActivity() {
        AppManager.getAppManager().currentActivity().finish()
    }

    //销毁所有Activity方法
    open fun removeALLActivity() {
        AppManager.getAppManager().finishAllActivity()
    }

    override fun finish() {
        super.finish()
        AppManager.getAppManager().finishActivity(this)
    }

}